/*
 * @Author: Shane
 * @Date: 2022-07-06 15:01:51
 * @LastEditTime: 2022-07-11 16:33:22
 * @LastEditors: Shane
 * @Description: 
 * @FilePath: /app-framework/common_widgets/lib/search/serach_bar.dart
 */

import 'package:flutter/material.dart';

class KKSearchBar extends StatelessWidget {
  final TextEditingController? editingController;
  final FocusNode? focusNode;
  final TextInputType? keyboardType;
  final Widget? icon;
  final String? hintText;
  final TextStyle? hintTextStyle;

  final Widget? search;
  final Function(String)? onSearchClick;
  final double height;
  final bool enabled;

  const KKSearchBar({
    Key? key,
    this.editingController,
    this.onSearchClick,
    this.hintText,
    this.hintTextStyle,
    this.search,
    this.icon,
    this.height = 36,
    this.enabled = true,
    this.focusNode,
    this.keyboardType,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      padding: const EdgeInsets.only(left: 10),
      decoration: BoxDecoration(
        color: const Color(0xFFF0F2F5),
        borderRadius: BorderRadius.circular(height / 2),
      ),
      child: Row(
        children: [
          icon ?? Container(),
          const VerticalDivider(
            color: Colors.transparent,
            width: 10,
          ),
          Expanded(
            child: enabled
                ? TextField(
                    style: const TextStyle(
                      color: Color(0xFF606266),
                      fontSize: 13,
                    ),
                    cursorColor: const Color(0xffC20014),
                    onSubmitted: (value) {
                      if (onSearchClick != null) {
                        onSearchClick!(editingController?.text ?? "");
                      }
                    },
                    textInputAction: TextInputAction.search,
                    decoration: InputDecoration(
                      hintText: hintText ?? "请输入",
                      hintStyle: hintTextStyle ??
                          const TextStyle(
                            color: Color(0xFFC0C4CC),
                            fontSize: 13,
                          ),
                      border: const OutlineInputBorder(
                        borderSide: BorderSide.none,
                      ),
                      contentPadding: const EdgeInsets.all(0),
                    ),
                    controller: editingController,
                    focusNode: focusNode,
                    keyboardType: keyboardType,
                  )
                : GestureDetector(
                    onTap: () {
                      if (onSearchClick != null) {
                        onSearchClick!(editingController?.text ?? "");
                      }
                    },
                    child: Text(
                      hintText ?? "请输入",
                      style: hintTextStyle ??
                          const TextStyle(
                            color: Color(0xFFC0C4CC),
                            fontSize: 13,
                          ),
                    ),
                  ),
          ),
          search ??
              ElevatedButton(
                onPressed: () {
                  if (onSearchClick != null) {
                    onSearchClick!(editingController?.text ?? "");
                  }
                },
                style: ButtonStyle(
                  elevation: MaterialStateProperty.all(0),
                  padding: MaterialStateProperty.all(EdgeInsets.zero),
                  backgroundColor:
                      MaterialStateProperty.all(const Color(0xffC20014)),
                  shape: MaterialStateProperty.all(
                    const StadiumBorder(
                      side: BorderSide.none,
                    ),
                  ),
                ),
                child: const Text(
                  "搜索",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14,
                  ),
                ),
              ),
        ],
      ),
    );
  }
}
